// pages/map/map.js
// 引入SDK核心类
var QQMapWX = require('../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        address:'',
        latitude: 30.61940,
        longitude: 104.12433
    },
    onLoad:function() {  
        // 实例化API核心类
        qqmapsdk = new QQMapWX({
          key: 'HLTBZ-IRCCO-OEFWI-SQWZW-3ASQQ-SGFYK'
        });
    },
    onShow: function () {
        let vm = this;
        vm.getUserLocation();
        // 调用接口
        qqmapsdk.search({
            keyword: '学校',
            success: function (res) {
                console.log(res);
            },
            fail: function (res) {
                console.log(res);
            },
            complete: function (res) {
                console.log(res);
            }
        });
    },
    getUserLocation: function () {
        let vm = this;
        wx.getSetting({
            success: (res) => {
                console.log(JSON.stringify(res))
                //res.authSetting['scope.userLocation'] == undefined 初始化进入页面
                //res.authSetting['scope.userLocation'] == false 非初始化进入页面，且未授权
                //res.authSetting['scope.userLocation'] == true 地理位置授权
                if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
                    wx.showModal({
                        title: '请求授权当前位置',
                        content: '需要获取您的地理位置，请确认授权',
                        success: function (res) {
                            if (res.cancel) {
                                wx.showToast({
                                    title: '拒绝授权',
                                    icon: 'none',
                                    duration: 1000
                                })
                            } else if (res.confirm) {
                                wx.openSetting({
                                    success: function (dataAu) {
                                        if (dataAu.authSetting["scope.userLocation"] == true) {
                                            wx.showToast({
                                                title: '授权成功',
                                                icon: 'success',
                                                duration: 1000
                                            })
                                            vm.getLocal();
                                        } else {
                                            wx.showToast({
                                                title: '授权失败',
                                                icon: 'none',
                                                duration: 1000
                                            })
                                        }                                       
                                    }
                                })
                            } 
                        }
                    })
                }else if (res.authSetting['scope.userLocation'] == undefined) {
                    vm.getLocal();
                } else {
                    vm.getLocal();
                }
            }
        })
    },
    getLocation: function () {
        let vm = this;
        wx.getLocation({
            type: 'gcj02',
            altitude: true,
            success: function (res) {
                console.log(JSON.stringify(res))
                var latitude = res.latitude
                var longitude = res.longitude
                var speed = res.speed
                var accuracy = res.accuracy;
                // vm.getLocal(latitude, longitude)
            },
            fail: function (res) {
                console.log('fail' + JSON.stringify(res))
            }
        })
    },
    getLocal:function(latitude,longitude){
        let vm = this;
        qqmapsdk.reverseGeocoder({
            location:{
                latitude: latitude,
                longitude: longitude
            },
            success:function(res){
                console.log(JSON.stringify(res));
                let province = res.result.ad_info.province
                let address = res.result.formatted_addresses.recommend
                vm.setData({
                    province:province,
                    address:address,
                    latitude: latitude,
                    longitude: longitude
                })
            },
            fail:function(res){
                console.log(res);
            },
            complete:function(res){
                // console.log(res);
            }
        });
    }
})